package cn.edu.gxnu.dsa.lab;

import cn.edu.gxnu.dsa.constant.ConstantSet;
import cn.edu.gxnu.dsa.queue.PriorityData;
import cn.edu.gxnu.dsa.queue.PriorityQueue;

public class FerrySimulator {
	private PriorityQueue vehicleQueue;
	private String vehicleSequence;
	private int busWeight;
	private int trunkWeight;
	private int vehicleSum;

	public FerrySimulator(String vehicleSequece) {
		this.vehicleSequence = vehicleSequece;
		vehicleQueue = new PriorityQueue();
		busWeight = 0;
		trunkWeight = 0;
		vehicleSum = 0;
	}

	public void start() {
		process();
		displayQueue();
	}

	private void process() {
		char vehicleType;
		for (int i = 0; i < vehicleSequence.length(); i++) {
			vehicleType = vehicleSequence.charAt(i);
			if (vehicleType == ' ') {
				continue;
			} else {
				switch (vehicleType) {
				case ConstantSet.TRUCK_TYPE:
					++trunkWeight;
					vehicleQueue.enQueue(new PriorityData(ConstantSet.TRUCK_TYPE, trunkWeight * 5));
					break;
				case ConstantSet.BUS_TYPE:
					busWeight++;
					if (busWeight == 5) {
						busWeight++;
					}
					vehicleQueue.enQueue(new PriorityData(ConstantSet.BUS_TYPE, busWeight));
					break;
				default:
					break;
				}
				vehicleSum++;
			}

		}
	}

	private void displayQueue() {
		PriorityData vehicleData;

		System.out.println("\rThe original sequence(Total: "+vehicleSum+" vehicle(s)):");
		System.out.println(vehicleSequence);
		System.out.println("\rThe simulation result:");

		int scale = vehicleQueue.length();
		int round = 0;
		for (int i = 0; i < scale; i++) {
			round++;
			vehicleData = (PriorityData) vehicleQueue.deQueue();
			System.out.print(vehicleData.getElement() + "/" + vehicleData.getPriority() + " ");
			if(round%ConstantSet.FERRY_CAPACITY == 0){
				System.out.print("<||> ");
			}
		}
		System.out.println("");

	}

}
